// 向地址栏中pend参数
const appendParams = (url : string, params : any) => {
	if (params) {
		let baseWithSearch = url.split('#')[0];
		let hash = url.split('#')[1];
		for (let key in params) {
			let attrValue = params[key];
			if (attrValue !== undefined) {
				let newParam = key + "=" + attrValue;
				if (baseWithSearch.indexOf('?') > 0) {
					let oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
					if (oldParamReg.test(baseWithSearch)) {
						baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
					} else {
						baseWithSearch += "&" + newParam;
					}
				} else {
					baseWithSearch += "?" + newParam;
				}
			}
		}

		if (hash) {
			url = baseWithSearch + '#' + hash;
		} else {
			url = baseWithSearch;
		}
	}
	return url;
}

export { appendParams }